package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.PdxmfsMs;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class PdxmfsMsForm extends BasePage implements Serializable {
    private GenericManager<PdxmfsMs, Long> pdxmfsMsManager;
    private PdxmfsMs pdxmfsMs = new PdxmfsMs();
    private Long id;

    public void setPdxmfsMsManager(GenericManager<PdxmfsMs, Long> pdxmfsMsManager) {
        this.pdxmfsMsManager = pdxmfsMsManager;
    }

    public PdxmfsMs getPdxmfsMs() {
        return pdxmfsMs;
    }

    public void setPdxmfsMs(PdxmfsMs pdxmfsMs) {
        this.pdxmfsMs = pdxmfsMs;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        pdxmfsMsManager.remove(pdxmfsMs.getId());
        addMessage("pdxmfsMs.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            pdxmfsMs = pdxmfsMsManager.get(id);
        } else {
            pdxmfsMs = new PdxmfsMs();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (pdxmfsMs.getId() == null || pdxmfsMs.getId() == 0);
        
        pdxmfsMs.setUsername(getRequest().getRemoteUser());
        pdxmfsMs.setOptdate(new Date());
        pdxmfsMs.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
        pdxmfsMsManager.save(pdxmfsMs);

        String key = (isNew) ? "pdxmfsMs.added" : "pdxmfsMs.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	 this.id=this.pdxmfsMs.getId();
             return edit();
        }
    }
} 